package jgl.vajra.management.part.vehicle.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ZoomControls;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;

import jgl.vajra.management.R;
import jgl.vajra.management.databinding.ActivityWaitingOrderBinding;
import jgl.vajra.management.manager.IntentManager;
import jgl.vajra.management.part.vehicle.contract.WaitingOrderContract;
import jgl.vajra.management.part.vehicle.pop.RewardPop;
import jgl.vajra.management.part.vehicle.viewModel.WaitingOrderViewModel;
import jgl.vajra.management.view.CountDownView;
import jgl.vajra.management.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.management.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.management.widget.utils.ToastUtils;

@CreateViewModel(WaitingOrderViewModel.class)
public class WaitingOrderActivity extends BaseMVVMActivity<WaitingOrderViewModel, ActivityWaitingOrderBinding> implements WaitingOrderContract.View {

    private BaiduMap mBaiduMap;
    private MapStatus mapBuild;
    private RewardPop rewardPop;

    @Override
    public int getLayoutId() {
        return R.layout.activity_waiting_order;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);

        //开始扩散
        mBinding.diffuseView.start();
        //结束扩散
//        mBinding.diffuseView.stop();


        mBinding.countDownView.setOnCountDownListener(new CountDownView.OnCountDownListener() {
            @Override
            public void countDownFinished() {
                //倒计时结束
                mBinding.countDownView.setCountdownTime(120);
                mBinding.countDownView.startCountDown();
            }
        });
        mBinding.countDownView.startCountDown();

        //禁止百度地图的手势
        UiSettings settings = mBinding.mapView.getMap().getUiSettings();
        settings.setAllGesturesEnabled(false);   //关闭一切手势操作
        settings.setOverlookingGesturesEnabled(false);//屏蔽双指下拉时变成3D地图
        settings.setZoomGesturesEnabled(false);//获取是否允许缩放手势返回:是否允许缩放手势

        mBinding.mapView.showScaleControl(false);  // 设置比例尺是否可见（true 可见/false不可见）
        mBinding.mapView.showZoomControls(false);  // 设置缩放控件是否可见（true 可见/false不可见）
        View child = mBinding.mapView.getChildAt(1); // 找到百度LOGO控件对象
        if ((child instanceof ImageView || child instanceof ZoomControls)) {
            child.setVisibility(View.INVISIBLE);  // 设置child这个控件是否可见（VISIBLE 可见/INVISIBLE不可见 /GONE 完全隐藏）
        }

        mBaiduMap = mBinding.mapView.getMap();
        LatLng ll = new LatLng(28.276159, 113.087089);
        mapBuild = new MapStatus.Builder()
                .target(ll)
                .zoom(18f)
                .build();
        //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapBuild);
        //改变地图状态
        mBaiduMap.animateMapStatus(mMapStatusUpdate);

        mBaiduMap.setOnMapStatusChangeListener(new BaiduMap.OnMapStatusChangeListener() {
            //地图状态开始改变。
            public void onMapStatusChangeStart(MapStatus status) {

            }

            @Override
            public void onMapStatusChangeStart(MapStatus mapStatus, int i) {

            }

            //地图状态改变结束
            public void onMapStatusChangeFinish(MapStatus status) {
                //改变结束之后，获取地图可视范围的中心点坐标
                LatLng latLng = status.target;
                //拿到经纬度之后，就可以反地理编码获取地址信息了
                GeoCoder geocoder = GeoCoder.newInstance();
                geocoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
                    @Override
                    public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
                    }

                    @Override
                    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
                        String name = "";
                        if (reverseGeoCodeResult.getPoiRegionsInfoList() != null && reverseGeoCodeResult.getPoiRegionsInfoList().size() != 0)
                            name = reverseGeoCodeResult.getPoiRegionsInfoList().get(0).getRegionName();
                        else if (reverseGeoCodeResult.getPoiList() != null && reverseGeoCodeResult.getPoiList().size() != 0)
                            name = reverseGeoCodeResult.getPoiList().get(0).name;

                        if (TextUtils.isEmpty(name)) {
                            ToastUtils.showShort("定位失败，请重试或手动输入地址");
                            return;
                        }
//                        mBinding.tvName.setText(name);
//                        mBinding.tvAddress.setText(String.format("%s%s", reverseGeoCodeResult.getAddressDetail().city, reverseGeoCodeResult.getAddressDetail().district));
                    }
                });

                //这个要放在后面！！！！！！！
                LatLng point = new LatLng(latLng.latitude, latLng.longitude);
                geocoder.reverseGeoCode(new ReverseGeoCodeOption().location(point));

            }

            //地图状态变化中
            public void onMapStatusChange(MapStatus status) {
            }
        });
    }

    @Override
    public void onResume() {
        mBinding.mapView.onResume();
        super.onResume();
    }

    @Override
    public void onPause() {
        mBinding.mapView.onPause();
        super.onPause();
    }

    @Override
    public void onDestroy() {
        mBinding.mapView.onDestroy();
        super.onDestroy();
    }

    //倒计时
    @Override
    public void onCountDown() {
        //订单
        IntentManager.toOrderStatusActivity(this);
    }

    //取消订单
    @Override
    public void onCancelOrder() {
        ToastUtils.showShort("取消成功");
    }

    //打赏小费
    @Override
    public void onReward() {
        if (rewardPop == null) {
            rewardPop = new RewardPop(this);
        }
        rewardPop.showPopupWindow(mBinding.getRoot());
    }
}
